<template>
    <div class="app-container">
        <el-card class="main-card">
            <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
                <el-form-item label="组件名称" prop="componentName">
                    <el-input v-model="queryParams.componentName" clearable placeholder="请输入组件名称" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button v-hasPermi="['ghxx:bVueComponent:list']" icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
                    <el-button v-hasPermi="['ghxx:bVueComponent:list']" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="main-card">
            <el-row :gutter="10" class="mb20">
                <el-col :span="1.5">
                    <el-button v-hasPermi="['ghxx:bVueComponent:add']" icon="el-icon-plus" plain size="mini" type="primary" @click="handleAdd">新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table :border="false" v-if="isType" v-loading="loading" :data="bVueComponentList" @selection-change="handleSelectionChange">
                <el-table-column align="center" type="selection" width="55" />
                <el-table-column align="center" label="组件名称" prop="componentName" />
                <el-table-column align="center" label="组件图片" prop="componentImage" width="100">
                    <template slot-scope="scope">
                        <image-preview :height="30" :src="scope.row.componentImage" :width="30" />
                    </template>
                </el-table-column>
                <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                    <template slot-scope="scope">
                        <el-button v-if="queryAuth(scope.row.deptIdStrs)" v-hasPermi="['ghxx:bVueComponent:edit']" icon="el-icon-edit" size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>
                        <el-button v-if="queryAuth(scope.row.deptIdStrs)" v-hasPermi="['ghxx:bVueComponent:remove']" icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- <el-row v-else v-loading="loading">
                <el-col v-for="item in bVueComponentList" :key="item.id" :span="cardSpan" style="padding: 2px">
                    <div style="width: auto; height: 300px">
                        <el-card :body-style="{ padding: '5px' }" style="height: 100%">
                            <span class="device-name">{{ item.isShare == 1 ? '公有组件' : '私有组件' }}</span>
                            <div @click="jumpDetail(item)">
                                <el-image :src="item.componentImage == null ? noImage : baseApi + item.componentImage" class="ztImage" title="编辑">
                                    <div slot="error" class="image-slot">
                                        <el-image src="http://111.231.1.12:81/static/img/logo-small.2437e089.png" />
                                    </div>
                                </el-image>
                            </div>
                            <div style="padding: 5px">
                                <span>{{ item.componentName }}</span>
                                <div class="bottom clearfix">
                                    <el-button v-hasPermi="['ghxx:bVueComponent:edit']" icon="el-icon-edit" type="text" @click="jumpDetail(item)">编辑</el-button>
                                    <el-button v-hasPermi="['ghxx:bVueComponent:remove']" icon="el-icon-delete" type="text" @click="handleDelete(item)">删除</el-button>
                                </div>
                            </div>
                        </el-card>
                    </div>
                </el-col>
            </el-row> -->
            <el-row v-else :gutter="30" v-loading="loading">
                <el-col v-for="item in bVueComponentList" :key="item.id" style="width: 20%; margin-bottom: 30px; text-align: center">
                    <div class="card-box-outer">
                        <el-card style="height: 300px; background-image: linear-gradient(#e9f1fc, #fefefe); box-sizing: border-box; margin-bottom: 20px; border: 0; position: relative" :body-style="{ padding: '5px' }">
                            <span class="device-name">{{ item.isShare == 1 ? '公有组件' : '私有组件' }}</span>
                            <!-- <span class="device-name">{{ item.deviceName }}</span> -->
                            <div class="card-image" @click="jumpDetail(item)">
                                <el-image :src="item.componentImage == null ? noImage : baseApi + item.componentImage" class="ztImage" title="编辑">
                                    <!-- <div slot="error" class="image-slot">
                                <el-image src="http://111.231.1.12:81/static/img/logo-small.2437e089.png" />
                            </div> -->
                                </el-image>
                                <div style="padding: 5px">
                                    <div class="card-name">{{ item.componentName }}</div>
                                    <!-- <svg-icon icon-class="qrCode" style="width:25px;height:25px;cursor:pointer;float: right;"
                    v-if="item.isShare == 1" @click="showCodeClick(item)" /> -->
                                    <div class="bottom clearfix">
                                        <el-button size="small" class="detail-btn" @click="jumpDetail(item)" v-hasPermi="['ghxx:bVueComponent:edit']">编辑</el-button>
                                        <el-button size="small" class="delete-btn" @click="handleDelete(item)" v-hasPermi="['ghxx:bVueComponent:remove']">删除</el-button>
                                    </div>
                                </div>
                            </div>
                        </el-card>
                    </div>
                </el-col>
            </el-row>
            <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page-size="10" :page-sizes="[10, 20, 30, 50]" :page.sync="queryParams.pageNum" :total="total" @pagination="getList" />
        </el-card>
        <!-- 添加或修改组件对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="500px">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="组件图片">
                    <image-upload v-model="form.componentImage" :limit="1" />
                </el-form-item>
                <el-form-item label="组件名称" prop="componentName">
                    <el-input v-model="form.componentName" placeholder="请输入组件名称" />
                </el-form-item>
                <el-form-item label="组件名称" prop="isShare">
                    <el-radio-group v-model="form.isShare">
                        <el-radio :label="0">私有</el-radio>
                        <el-radio :label="1">公有</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listBVueComponent, getBVueComponent, delBVueComponent, addBVueComponent, updateBVueComponent } from '@/views/zutai/api/zutai/bVueComponent';
import noImage from '../images/no-zt.png';

export default {
    name: 'BVueComponent',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            totalCard: 0,
            // 组件表格数据
            bVueComponentList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderByColumn: 'id',
                isAsc: 'desc',
                deptIdStrs: null,
                componentName: null,
                componentImage: null,
                isShare: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                componentName: [{ required: true, message: '组件名称不能为空', trigger: 'blur' }],
            },
            isType: false,
            baseApi: process.env.VUE_APP_BASE_API,
            noImage: noImage,
            isAuth: true,
            cardSpan: 4,
        };
    },
    created() {
        if (window.screen.width < 1680) {
            this.cardSpan = 6;
        } else {
            this.cardSpan = 4;
        }
        this.getList();
    },
    methods: {
        queryAuth(deptIdStrs) {
            if (!deptIdStrs) {
                return false;
            }
            if (deptIdStrs.indexOf(this.$store.getters.deptId) > -1) {
                return true;
            } else {
                return false;
            }
        },
        // 跳转详情页
        jumpDetail(row) {
            this.$router.push({
                path: '/zutai/deviceManageDetail',
                query: {
                    id: row.id,
                },
            });
        },
        /** 查询组件列表 */
        getList() {
            this.loading = false;
            listBVueComponent(this.queryParams).then((response) => {
                this.bVueComponentList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                deptIdStrs: null,
                componentName: null,
                componentImage: null,
                componentTemplate: null,
                componentStyle: null,
                componentScript: null,
                componentExternalLink: null,
                isShare: null,
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.isAuth = true;
            selection.forEach((element) => {
                if (this.queryAuth(element.deptIdStrs) == false) {
                    this.isAuth = false;
                }
            });
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加组件';
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getBVueComponent(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = '修改组件';
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateBVueComponent(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addBVueComponent(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除组件编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delBVueComponent(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'ghxx/bVueComponent/export',
                {
                    ...this.queryParams,
                },
                `bVueComponent_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
<style  lang="scss" scoped>
.ztImage {
    width: 100%;
    display: block;
    height: 200px;
}

.ztImage:hover {
    cursor: pointer;
    transform: scale(1.1);
    transition: transform 0.8s ease-in-out;
}

.device-name {
    background-color: #80f3da;
    display: inline-block;
    color: #409eff;
    text-align: center;
    height: 25px;
    line-height: 15px;
    font-size: 12px;
    padding: 4px 6px;
    position: absolute;
    margin: -5px;
    z-index: 2;
}

.bottom {
    margin-top: 13px;
    line-height: 12px;
    text-align: right;
}

.clearfix:before,
.clearfix:after {
    display: table;
    content: '';
}

.clearfix:after {
    clear: both;
}

.bottom {
    --font-grey: #606266;
    --success-color: #67c23b;
    --primary-color: #0147eb;
    margin-top: 13px;
    line-height: 12px;
    text-align: right;
}
</style>
